//
//  ZKRecommendTableViewCell.m
//  ZAKER
//
//  Created by yaohongchao on 16/2/28.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "ZKRecommendTableViewCell.h"
#import "UIImage+Color.h"
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"
#import "UIImage+RoundedCorner.h"

@interface ZKRecommendTableViewCell()
@property (nonatomic,weak) UIImageView *imgV;
@property (nonatomic,weak) UILabel     *lblTitle;
@property (nonatomic,weak) UILabel     *lblSubTitle;
@property (nonatomic,weak) UIView      *line;
@end

@implementation ZKRecommendTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setui];
    }
    return self;
}

-(void)setui{
    UIImageView *imgV = [[UIImageView alloc]init];
    [self addSubview:imgV];
    imgV.contentMode = UIViewContentModeScaleAspectFill;
    self.imgV = imgV;
    
    UILabel *lblTitle = [[UILabel alloc]init];
    [self addSubview:lblTitle];
    lblTitle.font = [UIFont systemFontOfSize:14];
    self.lblTitle = lblTitle;
    
    UILabel *lblSubTitle = [[UILabel alloc]init];
    [self addSubview:lblSubTitle];
    lblSubTitle.font = [UIFont systemFontOfSize:12];
    lblSubTitle.textColor = [UIColor lightGrayColor];
    self.lblSubTitle = lblSubTitle;
    
    UIView *line = [[UIView alloc]init];
    [self addSubview:line];
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    self.line = line;
    //设置约束
    [self setConstraints];
}

-(void)setConstraints{
    WS(weakSelf);
    
    [weakSelf.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15);
        make.width.height.mas_equalTo(60);
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
    
    [weakSelf.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.imgV.mas_right).offset(15);
        make.bottom.equalTo(weakSelf.mas_centerY).offset(-3);
        make.right.equalTo(weakSelf).offset(15);
        make.height.mas_equalTo(15);
    }];
    
    [weakSelf.lblSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.imgV.mas_right).offset(15);
        make.top.equalTo(weakSelf.mas_centerY).offset(3);
        make.right.equalTo(weakSelf).offset(15);
        make.height.mas_equalTo(15);
    }];
    
    [weakSelf.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(15);
        make.right.equalTo(weakSelf);
        make.bottom.equalTo(weakSelf).offset(-0.5);
        make.height.mas_equalTo(0.5);
    }];
}

-(void)refresh:(Block_Info*)blockInfo{
    WS(weakSelf);
    if([[SDImageCache sharedImageCache] diskImageExistsWithKey:blockInfo.large_pic]){
        self.imgV.image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:blockInfo.large_pic];
    }else{
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:blockInfo.large_pic] options:SDWebImageDownloaderHighPriority progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            if(finished && image){
                image = [image getRecommedImage:[UIColor colorWithHexString:blockInfo.block_color]];
                CGFloat width = image.size.width / 2;
                image = [UIImage jm_imageWithRoundedCornersAndSize:image.size JMRadius:JMRadiusMake(width, width, width, width) borderColor:[UIColor colorWithHexString:blockInfo.block_color] borderWidth:2 backgroundColor:nil backgroundImage:image withContentMode:UIViewContentModeScaleAspectFit];
                [[SDImageCache sharedImageCache]storeImage:image forKey:blockInfo.large_pic toDisk:YES];
                dispatch_sync(dispatch_get_main_queue(), ^{
                    weakSelf.imgV.image = image;
                });
            }
        }];
    }
    self.lblTitle.text = blockInfo.title;
    self.lblSubTitle.text =blockInfo.stitle;
}












@end
